package nickname

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/nickname"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    nicknameReq "github.com/flipped-aurora/gin-vue-admin/server/model/nickname/request"
)

type NicknamesService struct {
}

// CreateNicknames 创建Nicknames记录
// Author [piexlmax](https://github.com/piexlmax)
func (nicknamesService *NicknamesService) CreateNicknames(nicknames nickname.Nicknames) (err error) {
	err = global.GVA_DB.Create(&nicknames).Error
	return err
}

// DeleteNicknames 删除Nicknames记录
// Author [piexlmax](https://github.com/piexlmax)
func (nicknamesService *NicknamesService)DeleteNicknames(nicknames nickname.Nicknames) (err error) {
	err = global.GVA_DB.Delete(&nicknames).Error
	return err
}

// DeleteNicknamesByIds 批量删除Nicknames记录
// Author [piexlmax](https://github.com/piexlmax)
func (nicknamesService *NicknamesService)DeleteNicknamesByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]nickname.Nicknames{},"id in ?",ids.Ids).Error
	return err
}

// UpdateNicknames 更新Nicknames记录
// Author [piexlmax](https://github.com/piexlmax)
func (nicknamesService *NicknamesService)UpdateNicknames(nicknames nickname.Nicknames) (err error) {
	err = global.GVA_DB.Save(&nicknames).Error
	return err
}

// GetNicknames 根据id获取Nicknames记录
// Author [piexlmax](https://github.com/piexlmax)
func (nicknamesService *NicknamesService)GetNicknames(id uint) (nicknames nickname.Nicknames, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&nicknames).Error
	return
}

// GetNicknamesInfoList 分页获取Nicknames记录
// Author [piexlmax](https://github.com/piexlmax)
func (nicknamesService *NicknamesService)GetNicknamesInfoList(info nicknameReq.NicknamesSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&nickname.Nicknames{})
    var nicknamess []nickname.Nicknames
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Nickname_user != nil {
        db = db.Where("nickname_user = ?",info.Nickname_user)
    }
    if info.Nickname != "" {
        db = db.Where("nickname = ?",info.Nickname)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&nicknamess).Error
	return  nicknamess, total, err
}
